<template>
  <div>
    <div style="text-align: left;margin-left:2em">
      <el-button type="text" @click="openAddCustomer('editForm')" style="margin-right:20px" size="medium"><i class="el-icon-circle-plus-outline"></i>新增客户决策信息</el-button>
    </div>
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px">
      <el-row v-for="(item, index) in model.projectCustomerList" :key="index">
        <el-col :span="12">
          <el-form-item label="填写时间" :prop="'projectCustomerList.' + index + '.finishwritetime'">
            <el-date-picker v-model="item.finishwritetime" type="date" value-format="yyyy-MM-dd" :disabled="disableEditSubmit || disableSave"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户决策链支持度" :prop="'projectCustomerList.' + index + '.clientsupport'">
            <el-input v-model="item.clientsupport" :disabled="disableEditSubmit || disableSave"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="客户决策模式" :prop="'projectCustomerList.' + index + '.decisionmode'">
            <el-input v-model="item.decisionmode" type="textarea" :rows="4" maxlength="1000" show-word-limit :disabled="disableEditSubmit || disableSave"></el-input>
          </el-form-item>
        </el-col>
        <div style="text-align: left;margin-left:2em">
            <el-button type="text" @click="openUpdateCustomer('editForm', item)" style="margin-right:20px; margin-bottom: 2em" size="medium"><i class="el-icon-edit"></i>修改客户决策信息</el-button>
            <el-button type="text" @click="deleteProjectCustomer(index)" style="margin-right:20px; margin-bottom: 2em" size="medium"><i class="el-icon-remove-outline"></i>删除客户决策信息</el-button>
        </div>
      </el-row>
    </el-form>
    <tl-mpm-project-customer ref="editForm" :projectid="projectid" @refresh="refreshCustomer"></tl-mpm-project-customer>
  </div>
</template>

<script>
/** mixins */
import { mixin_edit } from '@tapui/mixins/mixin_edit'
import tlMpmProjectCustomer from '@/views/mpm/mpmProjectCustomerDecision/editCustomer'
/** 子组件 */
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_edit],
  components: { tlMpmProjectCustomer },
  data() {
    return {
      moduleTitle: '项目立项-客户决策',
      urlPrefix: '/mpm/projectCustomerDecision/',
      url: {
        loadCustomerInfo: '/mpm/projectCustomerDecision/loadCustomerInfo',
        delete: '/mpm/projectCustomerDecision/delete'
        // 额外的url
      },
      disableSave: true
    }
  },
  props: {
    projectid: {
      type: Number,
      default: 0
    }
  },
  methods: {
    loaded() {
      // 页面加载完毕执行
    },
    open() {
      this.$set(this.model, 'projectCustomerList', [{}])
      let that = this
      that.$nextTick(() => {
        that.$refs.form.clearValidate()
        if (that.isAdd === true) {
          if (that.forProjectChange) {
            that.queryData(that.projectid).then((_) => {
              that.loaded()
            })
          } else {
            that.loaded()
          }
        } else if (that.disableEditSubmit === false) {
          that.title = '编辑' + that.moduleTitle
          that.queryData(that.projectid).then((_) => {
            that.loaded()
          })
        } else {
          that.title = '查看' + that.moduleTitle
          that.queryData(that.projectid).then((_) => {
            that.$refs.form.clearValidate()
            that.loaded()
          })
        }
      })
    },
    queryData(projectid) {
      if (!this.url.loadCustomerInfo) {
        this.$tap.alert('请设置loadCustomerInfo地址!', 'warning')
        return
      }
      return getAction(this.url.loadCustomerInfo, { projectid: projectid }).then((res) => {
        if (res.success) {
          // 不显示空的客户决策信息
          let customer = res.result.filter((item) => item.clientsupport || item.decisionmode)
          this.$set(this.model, 'projectCustomerList', customer)
        } else {
          this.$tap.alert(res.message, 'warning')
        }
      })
    },
    // 删除客户决策信息
    deleteProjectCustomer(index) {
      if (this.model.projectCustomerList.length === 1) {
        this.$tap.alert('客户决策信息数量为1时，不允许删除')
      } else {
        this.$tap
        .confirm('确定删除数据吗？')
        .then((_) => {
            deleteAction(this.url.delete, { ids: this.model.projectCustomerList[index].id }).then((res) => {
            if (res.success) {
              this.$tap.message(res.message, 'success')
              this.refreshCustomer()
            } else {
              this.$tap.alert(res.message, 'warning')
            }
          })
        })
      }
    },
    // 打开新增页面
    openAddCustomer(editForm) {
      this.$refs[editForm].isAdd = true
      this.$refs[editForm].visible = true
      this.$refs[editForm].disableEditSubmit = false
    },
    // 打开编辑页面
    openUpdateCustomer(editForm, rowData) {
      this.$refs[editForm].isAdd = false
      this.$refs[editForm].visible = true
      this.$refs[editForm].disableEditSubmit = false
      this.$refs[editForm].id = rowData.id
    },
    // 刷新客户决策信息
    refreshCustomer() {
        this.open()
    }
  }
}
</script>
